package fr.isen.smssender.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import fr.isen.smssender.contact.*;
import fr.isen.smssender.sms.*;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: isen
 * Date: 19/01/12
 * Time: 11:45
 * To change this template use File | Settings | File Templates.
 */
public class DBManager {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "myDB";

    SmsSenderSQLite SOH;
    SQLiteDatabase DB;

    public DBManager(Context context){
        SOH = new SmsSenderSQLite(context, DATABASE_NAME, null, DATABASE_VERSION);
        DB = SOH.getWritableDatabase();
    }

    public Cursor getContactsCursor(){
        return DB.query(SmsSenderSQLite.TABLE_CONTACT,null,null,null,null,null,null);
    }

    public Cursor getSmsCursor(){
        return DB.query(SmsSenderSQLite.TABLE_SMS,null,null,null,null,null,null);
    }

    public  int getSmsIdByPositionInLV(int position){
        if (position == -1) return -1;
        Cursor cursor = getSmsCursor();
        cursor.moveToPosition(position);
        return (cursor.getInt(cursor.getColumnIndex(SmsSenderSQLite.COL_ID_SMS)));
    }

    public  int getContactIdByPositionInLV(int position){
        if (position == -1) return -1;
        Cursor cursor = getContactsCursor();
        cursor.moveToPosition(position);
        return (cursor.getInt(cursor.getColumnIndex(SmsSenderSQLite.COL_ID_SMS)));
    }

    public ArrayList<Contact> getContacts(){
        ArrayList<Contact> contacts = new ArrayList<Contact>();
        Cursor cursor = getContactsCursor();
        cursor.moveToFirst();

        for (int i = 0 ; i < cursor.getCount() ; i++){
            Contact result = new Contact(
                    cursor.getInt(cursor.getColumnIndex(SmsSenderSQLite.COL_ID)),
                    cursor.getString(cursor.getColumnIndex(SmsSenderSQLite.COL_NOM)),
                    cursor.getString(cursor.getColumnIndex(SmsSenderSQLite.COL_PRENOM)),
                    new Date(cursor.getString(cursor.getColumnIndex(SmsSenderSQLite.COL_BIRTHDAY))),
                    cursor.getString(cursor.getColumnIndex(SmsSenderSQLite.COL_NUMBER))
            );
            contacts.add(result);
        }
        return contacts;
    }

    public Contact getContact(int id){
        ArrayList<Contact> contacts = getContacts();
        for (Contact item : contacts){
            if (item.get_id() == id) return item;
        }
        return null;
    }

    public void setContact(Contact contact){
        ContentValues values = new ContentValues();

        values.put(SmsSenderSQLite.COL_NOM,contact.getNom());
        values.put(SmsSenderSQLite.COL_PRENOM,contact.getPrenom());
        values.put(SmsSenderSQLite.COL_BIRTHDAY,contact.getBirthday().toString());
        values.put(SmsSenderSQLite.COL_NUMBER,contact.getNumber());

        // Si le contact n'existe pas encore dans la BDD on fait un insert
        if (this.getContact(contact.get_id()) == null){
            DB.insert(SmsSenderSQLite.TABLE_CONTACT,null,values);
        }
        // S'il existe, on fait un update
        else {
            DB.update(SmsSenderSQLite.TABLE_CONTACT, values, SmsSenderSQLite.COL_ID +" = "+ contact.get_id() ,null);
        }
    }

    public void removeContact(int id){
        DB.delete(SmsSenderSQLite.TABLE_CONTACT, SmsSenderSQLite.COL_ID +" = "+ id, null);
    }


    public ArrayList<Sms> getSms(){
        ArrayList<Sms> listSms = new ArrayList<Sms>();
        Cursor cursor = DB.query(SmsSenderSQLite.TABLE_SMS,null,null,null,null,null,null);
        cursor.moveToFirst();

        for (int i = 0 ; i < cursor.getCount() ; i++){
            Sms result = new Sms(
                    cursor.getInt(cursor.getColumnIndex(SmsSenderSQLite.COL_ID_SMS)),
                    cursor.getString(cursor.getColumnIndex(SmsSenderSQLite.COL_MESSAGE))
            );
            listSms.add(result);
            if (!cursor.isLast()) cursor.moveToNext();
        }
        return listSms;
    }

    public Sms getSms(int id){
        ArrayList<Sms> listSms = getSms();
        for (Sms item : listSms){
            if (item.get_id() == id) return item;
        }
        return null;
    }

    public void setSms(Sms sms){
        ContentValues values = new ContentValues();

        values.put(SmsSenderSQLite.COL_MESSAGE,sms.getMessage());

        // Si le contact n'existe pas encore dans la BDD on fait un insert
        if (getSms(sms.get_id()) == null){
            DB.insert(SmsSenderSQLite.TABLE_SMS,null,values);
        }
        // S'il existe, on fait un update
        else {
            DB.update(SmsSenderSQLite.TABLE_SMS, values, SmsSenderSQLite.COL_ID_SMS +" = "+ sms.get_id() ,null);
        }
    }

    public void removeSms(int id){
        DB.delete(SmsSenderSQLite.TABLE_SMS, SmsSenderSQLite.COL_ID_SMS +" = "+ id, null);
    }
    public void setSmsToSend(Contact contact, Sms sms,Date date){
        Cursor cursor = DB.query(SmsSenderSQLite.TABLE_ToSend,null, SmsSenderSQLite.COL_ID_SEND_SMS + " = " + sms.get_id() +" AND " +SmsSenderSQLite.COL_ID_CONTACT,null,null,null,null);
        ContentValues values = new ContentValues();
        values.put(SmsSenderSQLite.COL_ID_SEND_SMS,sms.get_id());
        values.put(SmsSenderSQLite.COL_ID_CONTACT, contact.get_id());
        values.put(SmsSenderSQLite.COL_DATE,date.toString());
        if(cursor.getCount() == 0){
            DB.insert(SmsSenderSQLite.TABLE_ToSend,null,values);
        }else {
            DB.update(SmsSenderSQLite.TABLE_ToSend,values,SmsSenderSQLite.TABLE_ToSend+"."+SmsSenderSQLite.COL_ID_SEND+" = "+cursor.getString(cursor.getColumnIndex(SmsSenderSQLite.COL_ID_SEND)),null);
        }
    }

    public Cursor getSmsToSend(){
        return DB.query(SmsSenderSQLite.TABLE_ToSend,null,null,null,null,null,null);
    }
}